/**
 * Copyright 2014,Peak Tai 台俊峰(taijunfeng_it@sina.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.cenluan.tweet.comment;

import com.cenluan.me.call.Call;
import com.cenluan.tweet.Tweet;
import com.cenluan.user.User;
import com.cenluan.user.log.UserLog;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

/**
 * 微博评论
 * 
 * @author peak
 * 
 */
public class TweetComment extends Model<TweetComment> {

	private static final long serialVersionUID = 1L;

	public static final TweetComment dao = new TweetComment();

	/**
	 * 获取创建者
	 * 
	 * @return
	 */
	public User getCreater() {
		return User.dao.findById(getStr("createrId"));
	}

	/**
	 * 获取关联的微博
	 * 
	 * @return
	 */
	public Tweet getTweet() {
		return Tweet.dao.findById(getStr("tweetId"));
	}

	/**
	 * 删除，同时删除相关的被提到通知和用户动态
	 */
	@Override
	public boolean delete() {
		Call.dao.deleteByReferenceId(getStr("id"));
		UserLog.dao.deleteByReferenceId(getStr("id"));
		return super.delete();
	}

	/**
	 * 分页查询微博的评论
	 * 
	 * @param pn
	 * @param pz
	 * @param tweetId
	 * @return
	 */
	public Page<TweetComment> page(int pn, int pz, String tweetId) {
		StringBuilder sql = new StringBuilder();
		sql.append(" from t_tweet_comment ");
		sql.append(" where tweetId=? ");
		sql.append(" order by createTime desc ");
		return paginate(pn, pz, "select *", sql.toString(), tweetId);
	}
}
